home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / vector.lha / vector / cvector.h < prev    next >
C/C++ Source or Header  |  1991-11-23  |  4KB  |  174 lines

  1. #ifndef _CVECTOR_H
  2. #define _CVECTOR_H
  3.  
  4. #include <stream.h>
  5. #include <math.h>
  6. #include "vector_names.h"
  7.  
  8. // This is a template for a Cartesian vector class.
  9. // The usual operators are supplied.
  10.  
  11. #define CartesianVectorDeclare(T)  \
  12. class CVec(T) {                            \
  13. public:                                \
  14.     static errorHandler error;                    \
  15.     static void badvec(int, const char *);            \
  16.     static errorHandler setErrorHandler(errorHandler);        \
  17.                                 \
  18.     T &operator[](int i) { return x[i]; }            \
  19.     T  operator()(int i) const { return x[i]; }            \
  20.        operator T*() { return x; }                \
  21.                                 \
  22.     CVec(T)() {}                        \
  23.                                 \
  24.     CVec(T)(T xval, T yval, T zval) {                \
  25.     x[0] = xval; x[1] = yval; x[2] = zval;            \
  26.     }                                \
  27.                                 \
  28.     CVec(T)(const CVec(T) &v) {                    \
  29.     x[0] = v(0); x[1] = v(1); x[2] = v(2);            \
  30.     }                                \
  31.                                 \
  32.     T magnitude() const {                    \
  33.     return sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);    \
  34.     }                                \
  35.                                 \
  36.     CVec(T) &normalize();                    \
  37.                                 \
  38.     CVec(T) &operator+=(const CVec(T) &v) {            \
  39.     x[0] += v(0);                        \
  40.     x[1] += v(1);                        \
  41.     x[2] += v(2);                        \
  42.     return *this;                        \
  43.     }                                \
  44.                                 \
  45.     CVec(T) &operator-=(const CVec(T) &v) {            \
  46.     x[0] -= v(0);                        \
  47.     x[1] -= v(1);                        \
  48.     x[2] -= v(2);                        \
  49.     return *this;                        \
  50.     }                                \
  51.                                 \
  52.     CVec(T) &operator*=(T s) {                    \
  53.     x[0] *= s;                        \
  54.     x[1] *= s;                        \
  55.     x[2] *= s;                        \
  56.     return *this;                        \
  57.     }                                \
  58.                                 \
  59.     CVec(T) &operator/=(T s) {                    \
  60.     T recip = 1.0 / s;                    \
  61.     x[0] *= recip;                        \
  62.     x[1] *= recip;                        \
  63.     x[2] *= recip;                        \
  64.     return *this;                        \
  65.     }                                \
  66.                                 \
  67. protected:                            \
  68.     T        x[3];                        \
  69. };                                \
  70.                                 \
  71. /* -V */                            \
  72. inline CVec(T) operator-(const CVec(T) &v)            \
  73. {                                \
  74.     return CVec(T)(-v(0), -v(1), -v(2));            \
  75. }                                \
  76.                                 \
  77. /* s * V */                            \
  78. inline CVec(T) operator*(T s, const CVec(T) &v)            \
  79. {                                \
  80.     return CVec(T)(s * v(0), s * v(1), s * v(2));        \
  81. }                                \
  82.                                 \
  83. /* V * s */                            \
  84. inline CVec(T) operator*(const CVec(T) &v, T s) {        \
  85.     return s * v;                        \
  86. }                                \
  87.                                 \
  88. /* V / s */                            \
  89. inline CVec(T) operator/(const CVec(T) &v, T s)            \
  90. {                                \
  91.     T recip = 1.0 / s;                        \
  92.     return CVec(T)(v(0) * recip, v(1) * recip, v(2) * recip);    \
  93. }                                \
  94.                                 \
  95. /* V + V */                            \
  96. inline CVec(T) operator+(const CVec(T) &a, const CVec(T) &b)    \
  97. {                                \
  98.     return CVec(T)(a(0) + b(0), a(1) + b(1), a(2) + b(2));    \
  99. }                                \
  100.                                 \
  101. /* V - V */                            \
  102. inline CVec(T) operator-(const CVec(T) &a, const CVec(T) &b)    \
  103. {                                \
  104.     return CVec(T)(a(0) - b(0), a(1) - b(1), a(2) - b(2));    \
  105. }                                \
  106.                                 \
  107. /* V cross V */                            \
  108. inline CVec(T) operator^(const CVec(T) &a, const CVec(T) &b)    \
  109. {                                \
  110.     return CVec(T)(a(1) * b(2) - a(2) * b(1),            \
  111.           a(2) * b(0) - a(0) * b(2),            \
  112.           a(0) * b(1) - a(1) * b(0));            \
  113. }                                \
  114.                                 \
  115. /* V dot V */                            \
  116. inline T operator*(const CVec(T) &a, const CVec(T) &b)        \
  117. {                                \
  118.     return a(0) * b(0) + a(1) * b(1) + a(2) * b(2);        \
  119. }                                \
  120.                                 \
  121. /* V == V (no fuzz for comparison) */                \
  122. inline int operator==(const CVec(T) &a, const CVec(T) &b)    \
  123. {                                \
  124.     return (a(0) == b(0)) && (a(1) == b(1)) && (a(2) == b(2));    \
  125. }                                \
  126.                                 \
  127. /* V != V (no fuzz for comparison) */                \
  128. inline int operator!=(const CVec(T) &a, const CVec(T) &b) {    \
  129.     return !(a == b);                        \
  130. }                                \
  131.                                 \
  132. ostream &operator<<(ostream &o, const CVec(T) &v);
  133.  
  134. #define CartesianVectorImplement(T) \
  135.                             \
  136. errorHandler CVec(T)::error = &CVec(T)::badvec;        \
  137.                             \
  138. void CVec(T)::badvec(int code, const char *msg) {    \
  139.     cerr << "CVec(T): error " << code << ": "        \
  140.      << msg << endl;                \
  141. }                            \
  142.                             \
  143. errorHandler CVec(T)::setErrorHandler(errorHandler e) { \
  144.     errorHandler old = CVec(T)::error;            \
  145.     CVec(T)::error = e;                    \
  146.     return old;                        \
  147. }                            \
  148.                             \
  149. CVec(T) &CVec(T)::normalize() {                \
  150.     T mag = this->magnitude();                \
  151.                             \
  152.     if (mag != 0.0) {                    \
  153.     mag = 1.0 / mag;                \
  154.     x[0] *= mag;                    \
  155.     x[1] *= mag;                    \
  156.     x[2] *= mag;                    \
  157.     } else {                        \
  158.     (*error)(0, "magnitude=0 in normalize");    \
  159.     }                            \
  160.                             \
  161.     return *this;                    \
  162. }                            \
  163.                             \
  164. ostream &operator<<(ostream &o, const CVec(T) &v) {    \
  165.     return o << "( " << v(0) << ' '            \
  166.              << v(1) << ' ' << v(2) << " )";    \
  167. }
  168.  
  169. CartesianVectorDeclare(float);
  170.  
  171. typedef CVec(float) Vector;
  172.  
  173. #endif /*_CVECTOR_H*/
  174.